/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.security;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.security.Ugroup;
import src.services.security.UgroupModel;
import src.util.Messager;

/**
 *
 * @author Yusup Jauhari Shandi <chan at In.Visi Forever!>
 */

@ManagedBean
@SessionScoped
public class UgroupBean implements Serializable{
    private List<Ugroup> Ugroup;
    private Ugroup selectedData, insertedData;
    private boolean editMode;

    public UgroupBean() {
        UgroupModel helper = new UgroupModel();
        this.Ugroup = helper.findAll();
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public Ugroup getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Ugroup insertedData) {
        this.insertedData = insertedData;
    }

    public Ugroup getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Ugroup selectedData) {
        this.selectedData = selectedData;
    }

    public List<Ugroup> getUgroup() {
        return Ugroup;
    }

    public void setUgroup(List<Ugroup> Ugroup) {
        this.Ugroup = Ugroup;
    }
    
    
     public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Ugroup();
    
       // FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "New Data");  
      //  FacesContext.getCurrentInstance().addMessage(null, msg); 
    }
    
     public void openData() {  
        this.editMode = true;
        this.insertedData = src.domain.security.Ugroup.clone(selectedData);
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
         UgroupModel helper = new UgroupModel();
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.Ugroup.add(insertedData);
            else {
                int i = Ugroup.indexOf(selectedData);
                if (i <= Ugroup.size()) Ugroup.set(i, src.domain.security.Ugroup.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setUgroupId(null);
        } 
        
        /*FacesMessage msg = null;
        boolean success = true;  
             
        UgroupModel helper = new UgroupModel();
        Ugroup data = this.insertedData;
        if (this.insertedData == null) data = this.selectedData;
        
        success = helper.saveData(data);
        if (success) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penyimpanan berhasil.");
            
            if (this.editMode!=true)
                this.Ugroup.add(this.insertedData);
            
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penyimpanan tidak berhasil.");  
        }
        
        * 
        */
        //msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", data.getUgroupName()); 
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success); 
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;
        boolean success = true;  
       // msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", this.selectedData.getUgroupName()); 
        
        UgroupModel helper = new UgroupModel();
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penghapusan berhasil.");
           
            this.Ugroup.remove(this.selectedData);
            //this.selectedData = null;
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Penghapusan tidak berhasil.");  
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success); 
    }  
    
    
    
}
